﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Input;
using VMDS.Business.Entities;
using VMDS.GUIClient.Helper;
using VMDS.GUIClient.Model;

namespace VMDS.GUIClient.ViewModels.Management.Storages
{
    class AddStorageDialogViewModel:BaseViewModel
    {
        private bool _vis;
        public bool Visible
        {
            get { return _vis; }
            set { _vis = value;
                OnPropertyChanged(() => Visible);
            }
        }

        private bool _progressvis;
        public bool ProgressVisible
        {
            get { return _progressvis; }
            set
            {
                _vis = value;
                OnPropertyChanged(() => ProgressVisible);
            }
        }


        private string _name;
        public string StName
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged(() => StName);
                Add = command();
            }
        }

        private string _path;
        public string StPath
        {
            get { return _path; }
            set { _path = value; OnPropertyChanged(() => StPath);
                Add = command();
            }
        }

        private BaseCommand _add;

        public ICommand Add
        {
            get { return _add; }
            private set { _add = (BaseCommand)value;
                OnPropertyChanged(() => Add);
            }
        }

        private string _vmess;
        public string ValidationMessage
        {
            get { return _vmess; }
            set { _vmess = value;
                OnPropertyChanged(() => ValidationMessage);
            }
        }

        
        private bool canadd()
        {
            bool c1, c2, c3=false,c4=false;
            string ospath = StPath + "\\OS";
            c1 = !String.IsNullOrWhiteSpace(StName);
            c2 = !String.IsNullOrWhiteSpace(StPath);
            if (c2)
            {
                ValidationMessage = "Проверка пути...";
                c3 = Directory.Exists(StPath);
            }
            if (c3)
            {
                try
                {
                        Directory.CreateDirectory(ospath);
                        c4 = true;
                }
                catch
                {
                    c4 = false;
                }
            }
            if (!c4) ValidationMessage = "Нет прав доступа";
            if (!c3) ValidationMessage = "Указанный путь неверен";
            if (!c2) ValidationMessage = "Введите корректный путь";
            if (!c1) ValidationMessage = "Введите корректное имя";
            
            bool result = c2&&c1&&c3&&c4;
            if (result) ValidationMessage = "";
            return result;
        }

        BaseCommand command()
        {
            return new BaseCommand(add, canadd);
        }

        void add()
        {
            try
            {
                MsLocator.Client.AddStorage(new Storage() {Name = StName, Path=StPath});
                ManagerDataStore.Instance.LoadData();
                Visible = false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public AddStorageDialogViewModel()
        {
            Visible = true;
            _add = command();
        }
    }
}
